package charts;

import interfaces.TagDataGetter;

import java.util.Date;

import javax.swing.JPanel;

import model.Tools;

import org.jfree.data.category.DefaultCategoryDataset;

/**
 * This class is the base for every Chart in this app.
 * 
 * @author Manseer Marcel
 * @author Plavsic Maja
 * @author Pretsch Paul
 */
@SuppressWarnings("serial")
public abstract class Chart extends JPanel {


	protected DefaultCategoryDataset data = new DefaultCategoryDataset();

	/**
	 * used to fill a dataset with dummy data one row for every hashtag and one
	 * column per day hashtag-count is set to 0
	 * 
	 * @param since
	 * @param hashtags
	 */
	public void prepareDataSet(Date since, String... hashtags) {
		this.data.clear();

		since = Tools.IncDay(Tools.TruncateTime(since), 1);
		Date date = Tools.IncDay(Tools.TruncateTime(new Date()), 1);

		do {
			// request Data for single day
			for (String hashtag : hashtags) {
				this.data.addValue(0, hashtag,
						Tools.FormattedTwitterDate(since));
			}
			// get prev day
			since = Tools.IncDay(since, 1);
		} while (date.after(since));
	}

	/**
	 * Set the hashtag-count for a specific day and date. Data from a single
	 * {@link interfaces.TagDataGetter TagDataGetter} object is used.
	 * 
	 * @param twitterData
	 */
	public void setTagData(TagDataGetter twitterData, int oldCount) {
		String colKey = Tools.FormattedTwitterDate(twitterData.getDate());
		String rowKey = twitterData.getTag();

		if ((this.data.getRowIndex(rowKey) >= 0)
				&& (this.data.getColumnIndex(colKey) >= 0)) {
			this.data.setValue(twitterData.getCount(), rowKey, colKey);
		}
	}
}
